package org.jeecg.modules.cosmetic.constant.enums;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.cosmetic.constant.ExplainConstants;
import org.jeecg.modules.cosmetic.model.dto.AppraisalConclusionDTO;

import java.math.BigDecimal;
import java.text.MessageFormat;

/**
 * @author scx
 * @date 2022/7/15 9:55 上午
 */
public enum RinsingConclusionEnum {

    SAFE("安全", "Safe", "CIR评估结果显示，淋洗类化妆品中浓度为%s%s时，在化妆品中的使用是安全的", "本配方中添加量在安全用量以内。") {
        @Override
        public AppraisalConclusionDTO getConclusion(String rinsingContent, String iecicResideContent, String iecicRinsingContent, String elementPurity) {
            return AppraisalConclusionDTO.builder()
                    .authorityConclusion(String.format(this.getAuthorityMsg(), rinsingContent, "%"))
                    .evaluatedConclusion(this.getEvaluatedMsg())
                    .build();
        }
    },
    IRRITATION("安全（无刺激）", "Safe (non-irritation)", "CIR评估结果显示，淋洗类化妆品中浓度为%s时且当配方无刺激时，在化妆品中的使用是安全的", "本配方中添加量在安全用量以内。关于刺激性，{0}，可安全使用。") {
        @Override
        public AppraisalConclusionDTO getConclusion(String rinsingContent, String iecicResideContent, String iecicRinsingContent, String elementPurity) {
            AppraisalConclusionDTO appraisalConclusionDTO = AppraisalConclusionDTO.builder()
                    .authorityConclusion(String.format(this.getAuthorityMsg(), rinsingContent))
                    .build();
            checkIECICComparePurity(iecicResideContent, rinsingContent, elementPurity, appraisalConclusionDTO);
            return appraisalConclusionDTO;
        }
    },
    SENSITIZATION("安全（无致敏）", "Safe (non-sensitization)", "CIR评估结果显示，驻留类化妆品中浓度为%s时且当配方无致敏时，在化妆品中的使用是安全的", "本配方中添加量在安全用量以内。关于致敏性，{0}，可安全使用。") {
        @Override
        public AppraisalConclusionDTO getConclusion(String rinsingContent, String iecicResideContent, String iecicRinsingContent, String elementPurity) {
            AppraisalConclusionDTO appraisalConclusionDTO = AppraisalConclusionDTO.builder()
                    .authorityConclusion(String.format(this.getAuthorityMsg(), rinsingContent))
                    .build();
            checkIECICComparePurity(iecicResideContent, rinsingContent, elementPurity, appraisalConclusionDTO);
            return appraisalConclusionDTO;
        }
    },
    ALL("安全（无刺激无致敏）", "Safe (non-irritating and non-sensitizing)", "CIR评估结果显示，淋洗类化妆品中浓度为%s时且当配方无刺激无致敏时，在化妆品中的使用是安全的", "本配方中添加量在安全用量以内。关于刺激性和致敏性，{0}，可安全使用。") {
        @Override
        public AppraisalConclusionDTO getConclusion(String rinsingContent, String iecicResideContent, String iecicRinsingContent, String elementPurity) {
            AppraisalConclusionDTO appraisalConclusionDTO = AppraisalConclusionDTO.builder()
                    .authorityConclusion(String.format(this.getAuthorityMsg(), rinsingContent))
                    .build();
            checkIECICComparePurity(iecicResideContent, rinsingContent, elementPurity, appraisalConclusionDTO);
            return appraisalConclusionDTO;
        }
    },
    ;

    private String type;
    private String englishType;
    private String authorityMsg;
    private String evaluatedMsg;

    RinsingConclusionEnum(String type, String englishType, String authorityMsg, String evaluatedMsg) {
        this.type = type;
        this.englishType = englishType;
        this.authorityMsg = authorityMsg;
        this.evaluatedMsg = evaluatedMsg;
    }

    public static RinsingConclusionEnum getByType(String type) {
        for (RinsingConclusionEnum conclusionTypeEnum : RinsingConclusionEnum.values()) {
            if (type.contains(conclusionTypeEnum.type) || type.toLowerCase().contains(conclusionTypeEnum.getEnglishType().toLowerCase())) {
                return conclusionTypeEnum;
            }
        }
        return null;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getEnglishType() {
        return englishType;
    }

    public void setEnglishType(String englishType) {
        this.englishType = englishType;
    }

    public String getAuthorityMsg() {
        return authorityMsg;
    }

    public void setAuthorityMsg(String authorityMsg) {
        this.authorityMsg = authorityMsg;
    }

    public String getEvaluatedMsg() {
        return evaluatedMsg;
    }

    public void setEvaluatedMsg(String evaluatedMsg) {
        this.evaluatedMsg = evaluatedMsg;
    }

    public void checkIECICComparePurity(String iecicResideContent,
                                        String iecicRinsingContent,
                                        String elementPurity,
                                        AppraisalConclusionDTO appraisalConclusionDTO) {
        if (new BigDecimal(iecicRinsingContent).compareTo(new BigDecimal(elementPurity)) >= 0) {
            appraisalConclusionDTO.setHighestHistoryUsage(iecicRinsingContent);
            appraisalConclusionDTO.setEvaluatedConclusion(MessageFormat.format(this.getEvaluatedMsg(), ExplainConstants.C_RINSING_APPRAISAL_CONCLUSION_PARAM1));
        }
        if (StringUtils.isEmpty(iecicResideContent)) {
            throw new JeecgBootException("淋洗类产品-驻留含量为空无法校验");
        }
        if (new BigDecimal(iecicResideContent).compareTo(new BigDecimal(elementPurity)) >= 0) {
            appraisalConclusionDTO.setHighestHistoryUsage("驻留：" + iecicResideContent);
            appraisalConclusionDTO.setEvaluatedConclusion(MessageFormat.format(this.getEvaluatedMsg(), ExplainConstants.C_RINSING_APPRAISAL_CONCLUSION_PARAM2));
        }
        appraisalConclusionDTO.setEvaluatedConclusion(MessageFormat.format(this.getEvaluatedMsg(), ExplainConstants.C_BLANK_APPRAISAL_CONCLUSION));
    }


    public abstract AppraisalConclusionDTO getConclusion(String rinsingContent, String iecicResideContent, String iecicRinsingContent, String elementPurity);
}
